package com.util;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

import com.model.DataModel;

public class XmlResultString {
	
	  public String createXmlResult(Map refinedImageResults, ArrayList<String> googleImageResults, ArrayList<String> googleSrcImageResults, ArrayList<String> googleSrcURLs){
		  String xml=null;
		  try{
		  DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
		  DocumentBuilder db=dbf.newDocumentBuilder();
		  Document doc=db.newDocument();
		  Element root=doc.createElement("Results");
		  doc.appendChild(root);
		  Iterator iterator=refinedImageResults.keySet().iterator();
		  Element systemRecommendations=doc.createElement("picSEEKRecommendations");
		  root.appendChild(systemRecommendations);
		  for(int i=0;i<refinedImageResults.size();i++){
			  String tempImageUrl=(String)iterator.next();
			  Element tempUrl=doc.createElement("Image:"+(i+1));
			  systemRecommendations.appendChild(tempUrl);
			  Element url=doc.createElement("URL");
			  tempUrl.appendChild(url);
			  Text TextUrl =doc.createTextNode(tempImageUrl);
			  url.appendChild(TextUrl);
			  Element srcURL=doc.createElement("srcURL");
			  tempUrl.appendChild(srcURL);
			  Text TextSrcUrl =doc.createTextNode(googleSrcURLs.get(i));
			  srcURL.appendChild(TextSrcUrl);
			  
			  Element rank=doc.createElement("Rank");
			  tempUrl.appendChild(rank);
			  Text textRank =doc.createTextNode(""+refinedImageResults.get(tempImageUrl));
			  rank.appendChild(textRank);
		  }
		  
		  Element googleRecommendations=doc.createElement("SearchEngineRecommendations");
		  root.appendChild(googleRecommendations);
		  
		  for(int j=1;j<=googleImageResults.size();j++){
			  Element tempUrl=doc.createElement("Image:"+j);
			  googleRecommendations.appendChild(tempUrl);
			  Element url=doc.createElement("URL");
			  tempUrl.appendChild(url);
			  Text TextUrl =doc.createTextNode(googleImageResults.get(j-1));
			  url.appendChild(TextUrl);
			  
			  Element srcURL=doc.createElement("srcURL");
			  tempUrl.appendChild(srcURL);
			  Text TextSrcUrl =doc.createTextNode(googleSrcImageResults.get(j-1));
			  
			  srcURL.appendChild(TextSrcUrl);
			  Element rank=doc.createElement("Rank");
			  tempUrl.appendChild(rank);
			  Text textRank =doc.createTextNode("Null");
			  rank.appendChild(textRank);
		  }
		  
		  
		  
		  TransformerFactory tfac=TransformerFactory.newInstance();
		  Transformer tf=tfac.newTransformer();
		  StringWriter writer=new StringWriter();
		  javax.xml.transform.Result reslt= new StreamResult(writer);
		  Source src=new DOMSource(doc);
		  tf.transform(src	,reslt);
		  writer.close();
		  xml=writer.toString();
		  
		  
		  }catch (Exception e) {
			// TODO: handle exception
			  System.out.println(e.getMessage());
		}
		  return xml;
		  
	  }

}
